package option;

import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration.PropertiesReader;


public class InitParam {
	private static final PropertiesConfiguration PARAMS = new PropertiesConfiguration();
	// private static String fp =
	// GlobalVar.ServerIO.APPBASE_PATH.value()+"/care.properties";
	private static String fp = "care.properties";

	public static PropertiesConfiguration getParams() {
		return PARAMS;
	}

	public static String getProperty(String key) {
		String ret =  PARAMS.getString(key);
		if(ret == null){
			//use the default one
			return CAREConfig.getDefaultValue(key);
		}
		return ret;
	}

	public static boolean getBooleanProperty(String key) {
		String ret =  PARAMS.getString(key);
		if(ret == null){
			ret = CAREConfig.getDefaultValue(key);
		}
		return getProperty(key).trim().equalsIgnoreCase("true");
	}

	public InitParam(String propfile) {
		fp = propfile;
	}

	// open configuration file and set all parameters
	static {
		FileReader fs = null;
		try {
			fs = new FileReader(fp);
			PropertiesReader reader = new PropertiesReader(fs);
			PARAMS.load(reader);
			fs.close();
		} catch (Exception e) {
			System.err
					.println("Please create a (possibly empty) property file \"care.properties\"!\n"+
							"By default, the file is located in the directory that CARE is in.");
			e.printStackTrace();
		} finally {
			try {
				fs.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
